package com.jzo2o.foundations.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jzo2o.common.expcetions.ForbiddenOperationException;
import com.jzo2o.common.model.PageResult;
import com.jzo2o.foundations.mapper.RegionMapper;
import com.jzo2o.foundations.mapper.ServeItemMapper;
import com.jzo2o.foundations.mapper.ServeMapper;
import com.jzo2o.foundations.mapper.ServeSyncMapper;
import com.jzo2o.foundations.model.domain.Region;
import com.jzo2o.foundations.model.domain.Serve;
import com.jzo2o.foundations.model.domain.ServeItem;
import com.jzo2o.foundations.model.domain.ServeSync;
import com.jzo2o.foundations.model.dto.request.ServePageQueryReqDTO;
import com.jzo2o.foundations.model.dto.request.ServeSyncUpdateReqDTO;
import com.jzo2o.foundations.model.dto.request.ServeUpsertReqDTO;
import com.jzo2o.foundations.model.dto.response.ServeResDTO;
import com.jzo2o.foundations.service.IServeService;
import com.jzo2o.foundations.service.IServeSyncService;
import com.jzo2o.mysql.utils.PageHelperUtils;
import com.jzo2o.mysql.utils.PageUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PutMapping;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 服务同步表 服务实现类
 * </p>
 *
 * @author itcast
 * @since 2023-07-10
 */
@Service
public class ServeServiceImpl extends ServiceImpl<ServeMapper, Serve> implements IServeService {

    @Autowired
    private  ServeMapper serveMapper;
    @Autowired
    private ServeItemMapper serveItemMapper;
    @Autowired
    private RegionMapper regionMapper;



    @Override
    public PageResult<ServeResDTO> findByPage(ServePageQueryReqDTO servePageQueryReqDTO){
        return PageHelperUtils.selectPage(servePageQueryReqDTO,()->
                baseMapper.queryListByRegionId(servePageQueryReqDTO.getRegionId()));
    }
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void add(List<ServeUpsertReqDTO> dtoList){
        if(dtoList == null || dtoList.isEmpty()){
            throw new ForbiddenOperationException("添加失败,项目为空");
        }
        for(ServeUpsertReqDTO dto : dtoList){
            ServeItem serveItem = serveItemMapper.selectById(dto.getServeItemId());
            if(serveItem.getActiveStatus()!=2|| ObjectUtil.isNull(serveItem)){
                throw new ForbiddenOperationException("添加失败,项目服务状态有误");
            }
            int count = count(Wrappers.<Serve>lambdaQuery().eq(Serve::getRegionId, dto.getRegionId())
                    .eq(Serve::getServeItemId, dto.getServeItemId()));
            if(count>0){
                throw new ForbiddenOperationException("添加失败,当前服务项目已存在");
            }
            Serve serve = BeanUtil.copyProperties(dto, Serve.class);
            Region region = regionMapper.selectById(dto.getRegionId());
            if(ObjectUtil.isNotNull(region)) {
                serve.setCityCode(region.getCityCode());
            }
            serveMapper.insert(serve);
        }
    }
    @Override
    public void deleteById(Long id){
        Serve serve = serveMapper.selectById(id);
        if(ObjectUtil.isNull(serve)||serve.getSaleStatus()!=0){
            throw new ForbiddenOperationException("删除失败,当前服务不是草稿状态");
        }
        serveMapper.deleteById(id);
    }
    @Override
    public void onSale(Long id){
        Serve serve = serveMapper.selectById(id);
        if(ObjectUtil.isNull(serve)||serve.getSaleStatus()==2){
            throw new ForbiddenOperationException("当前服务已经是上架状态");
        }
        ServeItem serveItem = serveItemMapper.selectById(serve.getServeItemId());
        if(ObjectUtil.isNull(serveItem)||serveItem.getActiveStatus()!=2){
            throw new ForbiddenOperationException("服务项目未启动");
        }
        serve.setSaleStatus(2);
        serveMapper.updateById(serve);
    }
    @Override
    public void offSale(Long id){
        Serve serve = serveMapper.selectById(id);
        if(ObjectUtil.isNull(serve)||serve.getSaleStatus()==1){
            throw new ForbiddenOperationException("当前服务已经是下架状态");
        }

        serve.setSaleStatus(1);
        serveMapper.updateById(serve);
    }
    @Override
    public void onHot(Long id){
      Serve serve=new Serve();
      serve.setId(id);
      serve.setHotTimeStamp(new Date().getTime());
        serve.setIsHot(1);
        serveMapper.updateById(serve);
    }
    @Override
    public void offHot(Long id){
       Serve serve=new Serve();
       serve.setId(id);
        serve.setIsHot(0);
        serveMapper.updateById(serve);
    }

}
